package com.board.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alipay.api.AlipayApiException;
import com.board.dto.*;
import com.board.form.*;
import com.board.param.*;
import com.board.service.SeatEatOrderService;
import com.board.util.JwtUtil;
import com.board.util.PageInfo;
import com.board.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 膳食系统-堂食订单表 前端控制器
 * </p>
 *
 * @author 小叶
 * @since 2024年04月04日
 */
@RestController
@RequestMapping("/seatEatOrder")
@Api(tags = "堂食订单接口")
public class SeatEatOrderController {
    @Autowired
    private SeatEatOrderService seos;


    /**
     * 用户创建堂食订单
     * @param form 传递的订单信息，包含菜品id和数量、餐厅和商店id、备注
     * @param token 新增订单的用户token信息
     * @return 返回新增的订单信息
     */
    @PostMapping("/createOrder")
    @ApiOperation("用户生成堂食订单")
    public Result createOrder(@RequestBody SeatOrderForm form, @RequestHeader String token){
        Integer userId = (Integer) JwtUtil.getData(token, "subject");
        SeatOrderParam param = BeanUtil.toBean(form, SeatOrderParam.class);
        param.setOrderType(0);
        SeatOrderDto dto = ((SeatOrderDto) seos.createOrder(param, userId));
        return Result.success("生成堂食订单成功",dto);
    }

    /**
     * 用户查询堂食订单
     * @param form 查询条件，包含时间段、商店、餐厅
     * @param token 用户token
     * @return 返回查询结果
     */
    @GetMapping("/userFindOrder")
    @ApiOperation("用户条件查询堂食订单")
    public Result userFindOrder(UserFindSeatOrderForm form, @RequestHeader String token){
        Integer userId = (Integer) JwtUtil.getData(token, "subject");
        List<SeatOrderDto> dtos = seos.userFindOrder(BeanUtil.toBean(form, UserFindSeatOrderParam.class),userId);
        return Result.success("查看堂食订单成功",dtos);
    }

    /**
     * 用户手动取消订单
     * @param form 取消的订单编号和订单类型
     * @param token 用户token
     * @return 返回取消的订单信息
     */
    @PostMapping("/cancelOrder")
    @ApiOperation("用户手动取消订单")
    public Result cancelOrder(@RequestBody CancelOrderForm form,@RequestHeader String token){
        Integer userId = (Integer) JwtUtil.getData(token, "subject");
        Object dto =  seos.cancelOrder(form,userId);
        return Result.success("取消订单成功",dto);
    }

    /**
     * 用户发起支付订单
     * @param form 支付的订单号和订单类型
     * @param token 用户token
     * @return 返回支付链接
     * @throws Exception 支付宝的支付方法抛出的异常
     */
    @PostMapping("/payOrder")
    @ApiOperation("用户发起支付订单")
    public Result payOrder(@RequestBody PayOrderForm form,@RequestHeader String token) throws Exception {
        Integer userId = (Integer) JwtUtil.getData(token, "subject");
        String payForm = seos.payOrder(form,userId);
        return Result.success("发起支付成功",payForm);
    }

    /**
     * 支付宝调用的堂食订单支付回调接口
     * @param request 支付宝传递过来的参数
     * @return 返回成功给到支付宝
     * @throws AlipayApiException
     */
    @PostMapping("/orderCallBack")
    @ApiOperation("堂食订单支付回调接口")
    public String orderCallBack(HttpServletRequest request) throws AlipayApiException {
        System.out.println("==================回调接口================");
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
//            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        params.put("order_type","0");
        seos.orderCallBack(params);
        return "SUCCESS";
    }

    /**
     * 用户进行取餐
     * @param orderNum 取餐的订单号
     * @param token 用户token
     * @return 返回的取餐信息
     */
    @PostMapping("/userGetFood")
    @ApiOperation("用户进行取餐")
    public Result userGetFood(String orderNum,@RequestHeader String token){
        Integer userId = (Integer) JwtUtil.getData(token, "subject");
        Integer orderType=0;
        SeatOrderDto dto = ((SeatOrderDto) seos.userGetFood(orderNum, userId, orderType));
        return Result.success("用户取餐成功",dto);
    }

    /**
     * 用户提交退款申请
     * @param form 退款提交的内容，包含订单号、退款理由、订单类型
     * @param token 用户token
     * @return 返回退款的订单信息
     * @throws AlipayApiException
     */
    @PostMapping("/refund")
    @ApiOperation("用户提交退款申请")
    public Result refund(@RequestBody OrderRefundForm form, @RequestHeader String token) throws AlipayApiException {
        Integer userId = (Integer) JwtUtil.getData(token, "subject");
        Object dto = seos.refund(BeanUtil.toBean(form, OrderRefundParam.class),userId);
        return Result.success("用户提交退款成功",dto);
    }

    /**
     * 用户申请平台介入
     * @param form 申请的订单编号和订单类型
     * @param token 用户token
     * @return 返回申请的订单信息
     */
    @PostMapping("/applyPlatform")
    @ApiOperation("用户申请平台介入")
    public Result applyPlatform(@RequestBody ApplyOrderForm form,@RequestHeader String token){
        Integer userId = (Integer) JwtUtil.getData(token, "subject");
        Object dto= seos.applyPlatform(BeanUtil.toBean(form,ApplyOrderParam.class),userId);
        return Result.success("申请平台介入成功",dto);
    }

    /**
     * 用户再来一份订单
     * @param form 选择的再来一份订单号和订单类型
     * @param token 用户token
     * @return 返回订单结果
     */
    @PostMapping("/onceMoreOrder")
    @ApiOperation("用户再来一份订单")
    public Result onceMoreOrder(@RequestBody OrdernumTypeForm form,@RequestHeader String token){
        Integer userId = (Integer) JwtUtil.getData(token, "subject");
        Object dto= seos.onceMoreOrder(BeanUtil.toBean(form,OrdernumTypeParam.class),userId);
        return Result.success("再来一份成功",dto);
    }


    /**
     * 商家查询订单
     * @param form 查询条件，包含菜品名字、订单类型、分页
     * @param token 查询的商家token
     * @return 返回查询结果
     */
    @GetMapping("/storeFindOrder")
    @ApiOperation("商家查询订单")
    public Result storeFindOrder(StoreFindOrderForm form, @RequestHeader String token){
        Integer storeId = (Integer) JwtUtil.getData(token, "store_id");
        PageInfo<SeatOrderDto> pageInfo =  seos.storeFindOrder(form,storeId);
        return Result.success("查询成功",pageInfo);
    }

    /**
     * 商家接受订单
     * @param form 接受的订单编号和订单类型
     * @param token 商家token
     * @return 返回接受的订单信息
     */
    @PostMapping("/acceptOrder")
    @ApiOperation("商家接受订单")
    public Result acceptOrder(@RequestBody OrdernumTypeForm form,@RequestHeader String token){
        Integer storeId = (Integer) JwtUtil.getData(token, "store_id");
        Object dto =  seos.acceptOrder(form,storeId);
        return Result.success("商家接受订单成功",dto);
    }

    /**
     * 商家拒绝接单
     * @param form 拒绝接单的订单编号、拒绝理由、订单类型
     * @param token 商家token
     * @return 返回拒绝接单的订单信息
     */
    @PostMapping("/storeRefuseStore")
    @ApiOperation("商家拒绝接单")
    public Result storeRefuseStore(@RequestBody OrderRefuseForm form, @RequestHeader String token){
        Integer storeId = (Integer) JwtUtil.getData(token, "store_id");
        Object dto= seos.storeRefuseStore(BeanUtil.toBean(form, OrderRefuseParam.class),storeId);
        return Result.success("商家拒绝接单成功",dto);
    }

    /**
     * 商家完成出餐堂食订单
     * @param orderNum 出餐的订单编号
     * @param token 用户token信息
     * @return 返回出餐的订单信息
     */
    @PostMapping("/storeOutOrder")
    @ApiOperation("商家出餐完成堂食订单")
    public Result storeOutOrder(String orderNum,@RequestHeader String token){
        Integer storeId = (Integer) JwtUtil.getData(token, "store_id");
        OutOrderDto dto =  seos.storeOutOrder(orderNum,storeId);
        return Result.success("商家出餐完成",dto);
    }

    /**
     * 商家审核退款通过
     * @param form 审核的订单编号和订单类型
     * @param token 商家token
     * @return 返回审核的订单信息
     */
    @PostMapping("/storeCheckRefundAgree")
    @ApiOperation("商家审核退款通过")
    public Result storeCheckRefundAgree(@RequestBody StoreCheckPassForm form,@RequestHeader String token){
        Integer storeId = (Integer) JwtUtil.getData(token, "store_id");
        StoreCheckParam param = BeanUtil.toBean(form, StoreCheckParam.class);
        param.setCheckState(0);
        Object dto = seos.storeCheckRefund(param,storeId);
        return Result.success("商家审核退款通过",dto);
    }

    /**
     * 商家审核退款不通过
     * @param form 审核的订单编号
     * @param token 商家 token
     * @return 返回审核的订单信息
     */
    @PostMapping("/storeCheckRefundRefuse")
    @ApiOperation("商家审核退款不通过")
    public Result storeCheckRefundRefuse(@RequestBody StoreCheckRefuseForm form,@RequestHeader String token){
        Integer storeId = (Integer) JwtUtil.getData(token, "store_id");
        StoreCheckParam param = BeanUtil.toBean(form, StoreCheckParam.class);
        param.setCheckState(1);
        Object dto = seos.storeCheckRefund(param, storeId);
        return Result.success("商家审核退款不通过",dto);
    }

    /**
     * 平台查询订单
     * @param form 平台查询的条件参数，包含订单编号、订单状态、餐厅商家名称、订单类型、分页
     * @return 返回查询的结果
     */
    @GetMapping("/platformFindOrder")
    @ApiOperation("平台查询订单")
    public Result platformFindOrder(PlatformFindForm form){
        PageInfo<PlatformFindOrderDto> pageInfo= seos.platformFindOrder(BeanUtil.toBean(form,PlatformFindParam.class));
        return Result.success("查询订单成功",pageInfo);
    }

    /**
     * 平台审核通过订单
     * @param form 审核的订单编号和订单类型
     * @return 返回审核的订单数据
     * @throws AlipayApiException
     */
    @PostMapping("/platformCheckOrderPass")
    @ApiOperation("平台通过退款订单")
    public Result platformCheckOrderPass(@RequestBody PlatformCheckOrderForm form) throws AlipayApiException {
        //0审核通过
        Object dto = seos.platformCheckOrder(form,0);
        return Result.success("平台审核通过订单",dto);
    }

    /**
     * 平台审核拒绝订单
     * @param form 审核的订单编号和订单类型
     * @return 返回审核的订单数据
     * @throws AlipayApiException
     */
    @PostMapping("/platformCheckOrderRefuse")
    @ApiOperation("平台拒绝退款订单")
    public Result platformCheckOrderRefuse(@RequestBody PlatformCheckOrderForm form) throws AlipayApiException {
        //1审核不通过
        Object dto = seos.platformCheckOrder(form,1);
        return Result.success("平台审核不通过订单",dto);
    }



}

